﻿$(document).ready(function(){
	addTimePeriod();
	$("#addHourBtn").click(function(){
		addTimePeriod();
	});
	$("#clearAll").click(function(){
		chrome.storage.sync.clear(function(){
			alert("全部清除...");
		});
	});
	$("#done").click(function(){
		var resultArray = [];
  		chrome.storage.sync.get({'value': ''}, function(items){
  			console.log("取值成功......");
  			console.log(items["value"]);
  			console.log("取值结束......");
  			if(items["value"] != ''){
				resultArray = items["value"];
  			}

	  		var totalLength = resultArray.length;

			var tempArray = {};
			var url = $("[name='url']").val();
			tempArray.url = url;
			//获取限制时间段
			var checkedDay = $('input[type="checkbox"][name="week"]:checked');
			var totalDay = checkedDay.length;
			var weekArray = Array();
			for(var i = 0; i < totalDay; i ++){
				weekArray[i] = checkedDay.eq(i).val();
			}

			var startTime = $('[name="startTime"]');
			var endTime = $('[name="endTime"]');
			var times = startTime.length;
			var periodArray = [];
			for(var i = 0; i < times; i = i + 2){
				var tempPeriodArray = {};
				var startArray = Array(startTime.eq(i).val(), startTime.eq(i + 1).val());
				var endArray = Array(endTime.eq(i).val(), endTime.eq(i + 1).val());
				tempPeriodArray.start = startArray;
				tempPeriodArray.end = endArray;
				periodArray.push(tempPeriodArray);
			}

			tempArray.week = weekArray;
			tempArray.periodTime = periodArray;

			resultArray.push(tempArray);

			chrome.storage.sync.set({'value': resultArray}, function() {
	  			console.log("设置成功......");
				console.log(resultArray);
	  			console.log("设置结束......");
	  		});
  		});
	})
});
function addTimePeriod(){
	var strPeriodTime = '<div class="timeSelected">' +
						'<font>选择开始时间:</font>' +
						'<select name="startTime" class="startHour"></select>' + 
						'<select name="startTime" class="startMin"></select>' + 
						'</div>' +
						'<div class="timeSelected">' +
						'<font>选择结束时间:</font>' + 
						'<select name="endTime" class="endHour"></select>' + 
						'<select name="endTime" class="endMin"></select>' +
						'</div>' + 
						'<div class="cutLine" style="width: 60%; margin-left: 40px;"></div>';
	$(".timeSelectedContent").append(strPeriodTime);

	for(var i = 0; i < 24; i ++)
	{
		var foo = i;
		if(foo < 10){
			foo = "0" + foo;
		}
		$(".startHour").append("<option value=" + i + ">" + foo + "点</option>");
		$(".endHour").append("<option value=" + i + ">" + foo + "点</option>");
	}
	for(var i = 0; i < 60; i = i + 10)
	{
		var foo = i;
		if(foo < 10){
			foo = "0" + foo;
		}
		$(".startMin").append("<option value=" + i + ">" + foo + "分</option>");
		$(".endMin").append("<option value=" + i + ">" + foo + "分</option>");
	}
}